/*
 * Copyright (C), 2015-2019
 * FileName: ConstantPool
 * Author:   zhao
 * Date:     2019/2/2 21:05
 * Description: 常量池
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaoblog.jjvm.classfile.constantpool;

import com.lizhaoblog.jjvm.classfile.ClassReader;

import java.util.Arrays;

/**
 * 〈一句话功能简述〉<br>
 * 〈常量池〉
 *
 * @author zhao
 * @date 2019/2/2 21:05
 * @since 1.0.0
 */
public class ConstantPool {

    private final int constantPoolSize;
    private ConstantInfo[] constantInfos;

    public ConstantPool(ClassReader reader) {
        constantPoolSize = reader.nextU2ToInt();
        constantInfos = new ConstantInfo[constantPoolSize];
        for (int i = 1; i < constantPoolSize; i++) {
            int tag = reader.nextU1toInt();
            ConstantInfo constInfo = BaseConstantInfo.createConstantInfo(tag, reader, this);
            constantInfos[i] = constInfo;
        }
    }

    public String getUtfStringByIndex(int index) {
        return constantInfos[index].getValue();
    }

    @Override
    public String toString() {
        return "ConstantPool{" +
                "constantPoolSize=" + constantPoolSize +
                ", constantInfos=" + Arrays.toString(constantInfos) +
                '}';
    }
}